package com.zerui.manager.app.device.executer;

import com.alibaba.cola.catchlog.CatchAndLog;
import com.zerui.cola.dto.SingleResponse;
import com.zerui.manager.device.Device;
import com.zerui.manager.device.domainservice.DeviceRepository;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;

/**
 * @Author wh
 * @create_time 2023-08-25 11:12
 */
@Component
@CatchAndLog
public class DeviceAddCmdExe {

    @Resource
    private DeviceRepository deviceRepository;

    public SingleResponse<Long> execute(Long deviceId) {

        Device device = deviceRepository.getByDeviceId(deviceId);
        if(ObjectUtils.isNotEmpty(device)){
            device.setEnableStatus(device.getEnableStatus() == 0 ? 1 : 0);
            deviceRepository.update(device);
            return SingleResponse.of(deviceId);
        }
        deviceRepository.add(Device.create(deviceId, 1, null));
        return SingleResponse.of(deviceId);

    }

}
